/**
 * @Author : woon
 * @Timestamp : 2016-06-29
 */

var page = sm("do_Page");
var app = sm("do_App");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var root = ui("$");
var circleUrl = require("url/circle");
var http = require("helper/http");
var imgTool = require("helper/picture");

var updateCircleUrl = circleUrl.url.base.update;
var httpUrl4Detail = circleUrl.url.base.detail;


//定义ui
var do_ALayout_camera = ui("do_ALayout_camera");

//点击-相机
do_ALayout_camera.on("touch", function(data, e) {
	imgTool.imgCut(selectImgBack);
});

var imgLogo = ui("imgLogo");
function selectImgBack(imgList){
	imgLogo.source=imgList[0];
	imgLogo.tag = imgList[0];
}
//andriod返回键
page.on("back", function() {
    app.closePage();
});

/****************处理页面-头部-begin*****************/
var headerAout = ui("headerAout");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);

//左上角-返回
ui(addheader+".headerLeft").on("touch", function() {
    app.closePage()
});

var header = ui("header");
header.bindData(hashdata);
hashdata.addData({"labelTitle" : "修改一个圈子"});
header.refreshData();

//右上角-确定（提交）
var headerRight = ui(addheader+".headerRight");
var headerRight_lableNext = ui(addheader+".labelNext");
headerRight_lableNext.text = "提交";
headerRight_lableNext.visible = true;

//5秒防止多次点击
headerRight.on("touch", "", 5000, function() {
	submitUpdateCircle();
})

/****************处理页面-头部-end*****************/
var layout_main = ui("do_LinearLayout_main");

var addcircleRemark = layout_main.add("circleRemark", "source://view/reuse/form/mutiTextarea.ui",0,0);
var circleRemark = ui("circleRemark");
hashdata.addData({
    "textAreaHint":'圈子内容'
});
circleRemark.bindData(hashdata);
circleRemark.refreshData();

/**圈子标签**/
var addcircleTag = layout_main.add("circleTag", "source://view/reuse/form/mutiInput.ui",0,0);
var circleTag = ui("circleTag");
circleTag.bindData(hashdata);
hashdata.addData({
    "textTitle" : "圈子标签",
    "hint":'圈子的关键词,以逗号隔开'
});
circleTag.refreshData();

var addcircleName = layout_main.add("circleName", "source://view/reuse/form/mutiInput.ui",0,0);
var circleName = ui("circleName");
circleName.bindData(hashdata);
hashdata.addData({
    "textTitle" : "圈子名称",
    "hint":'最多10个文字'
});
circleName.refreshData();
circleName.enabled = false;

var circleId = null;
var oldLogo = null;
var categoryId = null;//分类id
var categoryName = '';
page.on("loaded", function() {
	circleId = page.getData().circleId;
	getCircleInfo();
});

/**圈子类别**/
var addtxtCategory = layout_main.add("txtCategory", "source://view/reuse/form/mutiLabel.ui",0,0);
var txtCategory = ui("txtCategory");
txtCategory.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "分类",
    "labelText":'',
    "labelTipsVisible":false,
    "labelTips":""
});
txtCategory.refreshData();
var addCategoryPicker = ui("ALayout_root").add("typeList","source://view/circle/base/manage/category.ui",0,0);
txtCategory.on("touch","",300,function(){
	page.hideKeyboard();
	http.post(circleUrl.url.category.levle1List, {}, function(data) {
		page.fire("categoryList",data.levle1List);
		page.fire("categorys","true");
	})
});
page.on("refreshCategory4Circle",function(data){
	var typeCategoryStr = ui(addtxtCategory+".labelText");
	if(data){
		if(data.sid != null){//如果有二级分类
			categoryId = data.sid;
			categoryName = data.sname;
			typeCategoryStr.text = data.bname + " / " + data.sname;
		}else if(data.bid != null){//如果只有一级分类
			categoryId = data.bid;
			categoryName = data.bname;
			typeCategoryStr.text = data.bname;
		}else{
			categoryId = null;
			categoryName = '';
			typeCategoryStr.text = '';
		}
	}else{
		categoryId = null;
		categoryName = '';
		typeCategoryStr.text = '';
	}
});
//加载圈子信息
function getCircleInfo(){
	http.post(httpUrl4Detail,{id:circleId},function(data){
		var circle = data.circleBase;
		if(circle){
			ui(addcircleName+".textInput").text = circle.name;
			ui(addcircleTag+".textInput").text = circle.tagName;
			ui(addcircleRemark+".textArea").text = circle.remark;
			categoryId = circle.categoryId;
			categoryName = circle.categoryName;
			ui(addtxtCategory+".labelText").text = categoryName;
			
			imgLogo.source = circle.fullLogo;
			imgLogo.tag = circle.logo;//用于临时存放logo
			oldLogo = circle.logo;
		}
	});
}

function submitUpdateCircle(){
	page.hideKeyboard();
	var logoPath = imgLogo.source;
	if(logoPath=="" || logoPath ==null || logoPath.indexOf("source://")>-1){
		nf.toast("请添加图片");
		return;
	}
	if(categoryId ==null){
		nf.toast("请选择分类");
		return;
	}
	var circleNameText=ui(addcircleName+".textInput");
	var circleRemarkText = ui(addcircleRemark+".textArea");
	var circleTagNameText = ui(addcircleTag+".textInput");
	var tagName = circleTagNameText.text.trim().replace(/\，/g , ',');//所有的中文逗号替换为英文逗号
	var circle = {
			"id":circleId,
			"name":circleNameText.text,
			"remark":circleRemarkText.text,
			"tagName":tagName,
			"logo":oldLogo
			};
	if(categoryId !=null){
		circle.categoryId = categoryId;
		circle.categoryName = categoryName;
	}
	if(imgLogo.tag == oldLogo){//图片没有修改
		http.post(updateCircleUrl,circle,function(data){
			nf.toast("修改圈子成功");
			app.closePage({"circle_reflesh":true});
		});
	}else{
		http.upload(logoPath,"circle",function(imgDatas){
			var string4imgs = imgDatas.path;//返回图片云端路径（后部份）用分号隔开
			if(string4imgs==""){
				nf.toast("请添加图片");
				return;
			}
			circle.logo = string4imgs;//更换新的图片
			http.post(updateCircleUrl,circle,function(data){
				nf.toast("修改圈子成功");
				app.closePage({"circle_reflesh":true});
			});
		});
	}
}
//点击背景隐藏输入键盘
root.on("touch",function(){
	page.hideKeyboard();
})